package com.ym.controller.admin;

import com.ym.common.utils.SourceFileUtil;
import com.ym.core.config.ConfigCenterProperties;
import com.ym.core.domain.APIResponse;
import com.ym.medical.domain.param.SysUserPwdParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author wenbo
 * @Date 2020/3/2 14:26
 **/
@Api(tags = "admin-pc登录状态密码修改")
@RestController
@RequestMapping("/sys/user/")
public class SysUserPwdUpdateController {

	@Autowired
	private ConfigCenterProperties centerProperties;

	@Autowired
	private RestTemplate restTemplate;

	@ApiOperation("pc登录状态用户密码更改")
	@PostMapping("/updatePwd")
	public APIResponse updatePwd(
			@ApiParam(name = "pwdParam", value = "密码相关", required = true)
			@RequestBody SysUserPwdParam pwdParam) throws Exception {
		Map<String, Object> param = new HashMap<>();
		param.put("userId", pwdParam.getUserId());
		param.put("oldPwd", pwdParam.getOldPwd());
		param.put("newPwd", pwdParam.getNewPwd());
		String url = centerProperties.getAuthSystemApi()
				.getUpdatePwd();
		String appKey = SourceFileUtil.source("authKey.txt");
		HttpHeaders headers = SourceFileUtil.restTemplateHeader(appKey);
		HttpEntity entity = new HttpEntity<>(param, headers);
		return restTemplate.postForObject(url, entity, APIResponse.class, param);
	}
}
